<template>
  <div class="system-role-dialog-container">
    <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="600px" @close="closeDialog" :close-on-click-modal="false">
      <el-form ref="dialogFormRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="100px"
        v-loading="state.dataLoading" label-position="right" style="width: 400px;">

        <el-form-item label="账号" prop="account">
          <el-input v-model="state.ruleForm.account" placeholder="请输入账号" clearable></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="state.ruleForm.name" placeholder="请输入名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="state.ruleForm.phone" placeholder="请输入电话" clearable></el-input>
        </el-form-item>
        <el-form-item label="Email" prop="email">
          <el-input v-model="state.ruleForm.email" placeholder="请输入Email" clearable></el-input>
        </el-form-item>
        <!-- <el-form-item label="Email" prop="department_id">
          <el-input v-model="state.ruleForm.department_id" placeholder="请输入Email" clearable></el-input>
        </el-form-item> -->
        <el-form-item label="所属部门" prop="department_id">
          <el-select v-model="state.ruleForm.department_id" placeholder="请选择所属部门" size="default" clearable filterable style="width:300px">
            <el-option v-for="(item, index) in state.departmentList" :key="index" :label="item.name"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog()" size="default">取 消</el-button>
          <el-button type="primary" :loading="state.btnLoading" @click="onSubmit(dialogFormRef)"
            size="default">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="systemRoleDialog">
import { systemApi } from '/@/api/system'
import { ElMessage } from 'element-plus';
import { toolsApi } from '/@/api/tools'
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const dialogFormRef = ref();
const state = reactive({
  userId: null,
  ruleForm: {
    account: '',
    name: '',
    phone: '',
    email: '',
    department_id: '',
  },
  rules: {
    account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
    name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
    phone: [{ required: true, message: '请输入电话', trigger: 'blur' }],
    department_id: [{ required: true, message: '请选择部门', trigger: 'change' }],
  },
  dialog: {
    isShowDialog: false,
    title: '新增用户',
    submitTxt: '',
  },
  userDetail: null,
  dataLoading: false,
  roleList: [],
  btnLoading: false,
  hospitalAreaList: [],
  departmentList: []
});

// 打开弹窗
const openDialog = (row) => {

  state.dialog.isShowDialog = true;
  if (row) {
    const departmentIdList = []
    state.userId = row.id;
    state.dialog.title = '修改用户';
    state.ruleForm.account = row.account
    state.ruleForm.name = row.name
    state.ruleForm.phone = row.phone
    state.ruleForm.email = row.email
    // state.ruleForm.department_id = row.department_id

    if(row.department && row.department.length > 0){
      row.department.forEach(item=>{
        departmentIdList.push(item.id)
      })
    }
    state.ruleForm.department_id = row.department_id

  } else {
    state.userId = null;
    state.dialog.title = '新增用户';
  }

};
// 关闭弹窗
const closeDialog = () => {
  dialogFormRef.value.resetFields();
  state.dialog.isShowDialog = false;
};


// 提交
const onSubmit = () => {
  dialogFormRef.value.validate((valid, fields) => {
    if (valid) {
      state.btnLoading = true
      const apiData = JSON.parse(JSON.stringify(state.ruleForm))
      if (state.userId) { // 编辑
        apiData.id = state.userId
        systemApi().editUser(apiData).then(res => {
          state.btnLoading = false
          ElMessage.success('操作成功');
          closeDialog();
          emit('refresh');
        }).catch(() => {
          state.btnLoading = false
          closeDialog();
          emit('refresh');
        })
      } else {
        systemApi().addUser(apiData).then(res => {
          state.btnLoading = false
          ElMessage.success('操作成功');
          closeDialog();
          emit('refresh');
        }).catch(() => {
          state.btnLoading = false
          closeDialog();
          emit('refresh');
        })
      }

    }

  })


};

// 获取病区
const getHospitalArea = () => {
  toolsApi().hospitalArea().then(res => {
    state.hospitalAreaList = res.data
  })
}

// 获取科室
const getDepatment = () => {
  toolsApi().departmentList({ all: 1 }).then(res => {
    state.departmentList = res.data
  })
}


// 暴露变量
defineExpose({
  openDialog,
});


onMounted(() => {
  getHospitalArea()
  getDepatment()
});
</script>

<style scoped lang="scss"></style>
